package org.apache.lucene.analysis.thesaurus;

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.URLDecoder;

import org.apache.log4j.Logger;

import com.yeqiangwei.commons.util.Constants;

/**
 * Title: TreeNode Description: Factory to initialize WordTree Copyright:
 * Copyright (c) 2007
 * 
 * @author Jolestar
 * @version 1.0
 * 
 */
public class WordTreeFactory {

	private static final Logger logger = Logger.getLogger(WordTreeFactory.class);

	private static WordTree instance = null;

	public static String WORD_PATH = null;

	private static String WEB_INF_PATH = null;

	private static String REAL_PATH = null;
	
	public static void setWordPath(String path){
		WORD_PATH = path;
	}

	public static WordTree initialize() throws IOException {
		instance = null;
		return getInstance();
	}
	
	public static WordTree getInstance() throws IOException {
		if (instance == null) {
			logger.info("词库尚未被初始化，开始初始化词库.");
			long begin = System.currentTimeMillis();
			instance = buildChineseWordTree();
			long end = System.currentTimeMillis();
			logger.info("初始化词库结束。用时:" + (end - begin) + "毫秒;");
			logger.info("共添加" + instance.getWordCount() + "个词语。");
		}
		return instance;
	}

	private static WordTree buildChineseWordTree() throws IOException {
		if (WORD_PATH == null) {
			if (REAL_PATH == null || WEB_INF_PATH == null) {
				String path = Constants.class.getProtectionDomain().getCodeSource().getLocation().getPath();
				logger.debug("getDefaultClassLoader().getResource(\"\").getPath(): " + path);
				try {
					String tmp = path.substring(0, path.lastIndexOf("WEB-INF"));
					File webRoot = new File(URLDecoder.decode(tmp, "UTF-8"));
					REAL_PATH = webRoot.getAbsolutePath() + File.separator;
					if (!REAL_PATH.endsWith("WEB-INF" + File.separator)) {
						WEB_INF_PATH = REAL_PATH + "WEB-INF" + File.separator;
					}
				} catch (Exception e) {
					logger.error(e.toString());
				}
			}
			WORD_PATH = WEB_INF_PATH + "index" + File.separator + "word.txt";
		}
		WordTree tree = WordTree.createDefault();
		logger.info(WORD_PATH);
		LineNumberReader reader = new LineNumberReader(new InputStreamReader(new FileInputStream(new File(WORD_PATH)), "UTF-8"));
		try {
			String line = null;
			while ((line = reader.readLine()) != null) {
				if (line.indexOf("#") < 0 && line.length() > 0) {
					tree.addChineseWord(line);
				}
			}
		} catch (Exception e) {
			logger.error(e.toString());
		} finally {
			if (reader != null) {
				reader.close();
			} else {
				logger.debug("词库为空！！！");
			}
		}
		return tree;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

}
